﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SICON_INT.PaginasWebs
{
    public partial class frm_Empresas : System.Web.UI.Page
    {
        /// <summary>
        /// The form accion
        /// </summary>
        static string FormAccion = "";
        /// <summary>
        /// The mensaje error
        /// </summary>
        public string MensajeError = "";
        /// <summary>
        /// The codigo
        /// </summary>
        static int codigo = 0;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["FormAccion"] != null)
                {
                    FormAccion = Session["FormAccion"].ToString();

                }

                if (Session["Codigo"] != null)
                {
                    codigo = Convert.ToInt32(Session["Codigo"]);

                }
                if (!IsPostBack)
                {
                    Cargar_Combos();
                    dlBaseDatos.SelectedValue = "1";
                    dlODBC.SelectedValue = "1";
                    dlEsqPlanta.SelectedValue = "1";
                    if (FormAccion == "Editar")
                    {
                        if (codigo != 0)
                        {
                            CargarEditar();
                        }
                    }

                }

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        private void CargarEditar()
        {
            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            WSGestion.MOD_Empresa obj = new WSGestion.MOD_Empresa();
            obj = wsgest.SEL_EmpresaByID(codigo);
            txtEmpresa.Value = obj.EMP_nombre;
            dlBaseDatos.SelectedValue = obj.EMP_codbasedatos.ToString();
            dlEsqPlanta.SelectedValue = obj.EMP_codeesquemaplanta.ToString();
            dlODBC.SelectedValue = obj.EMP_cododbc.ToString();
            txtOwner.Value = obj.EMP_owner;
        }

        private void Cargar_Combos()
        {
            try
            {
                WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                List<WSGestion.MOD_BaseDatos> ListaBD = wsgest.SEL_BASEDATOS().ToList();
                this.dlBaseDatos.DataSource = ListaBD;
                this.dlBaseDatos.DataValueField = "BDD_CODBD";
                this.dlBaseDatos.DataTextField = "BDD_NOMBREBD";
                this.dlBaseDatos.DataBind();

                List<WSGestion.MOD_EsqPlanta> ListaEsqPlanta = wsgest.SEL_ESQPLANTA().ToList();
                this.dlEsqPlanta.DataSource = ListaEsqPlanta;
                this.dlEsqPlanta.DataValueField = "ESQ_CODESQPLANTA";
                this.dlEsqPlanta.DataTextField = "ESQ_NOMBREESQPLANTA";
                this.dlEsqPlanta.DataBind();

                List<WSGestion.MOD_ODBC> ListaODBC = wsgest.SEL_ODBC().ToList();
                this.dlODBC.DataSource = ListaODBC;
                this.dlODBC.DataValueField = "ODBC_CODODBC";
                this.dlODBC.DataTextField = "ODBC_NOMBRE";
                this.dlODBC.DataBind();

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }


        }

        protected void btnAgregar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (FormAccion == "Agregar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_Empresa Empresa = new WSGestion.MOD_Empresa();

                    Empresa.BDD_nombre = txtEmpresa.Value;
                    Empresa.EMP_codeesquemaplanta = Convert.ToInt32(dlEsqPlanta.SelectedValue.ToString());
                    Empresa.EMP_cododbc = Convert.ToInt32(dlODBC.SelectedValue.ToString());
                    Empresa.EMP_codbasedatos = Convert.ToInt32(dlBaseDatos.SelectedValue.ToString());
                    Empresa.EMP_owner = txtOwner.Value;
                    if (wsgest.CRE_Empresa(Empresa))
                    {
                            //Response.Redirect("GridUsuarios.aspx");
                        Server.Transfer("frm_GridEmpresas.aspx");
                    }
                    else
                    {
                        MensajeError = "Procedimiento Almacenado no aceptado";
                    }
                }
                if (FormAccion == "Editar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_Empresa Empresa = new WSGestion.MOD_Empresa();

                    Empresa.EMP_codowner = codigo;
                    Empresa.BDD_nombre = txtEmpresa.Value;
                    Empresa.EMP_codeesquemaplanta = Convert.ToInt32(dlEsqPlanta.SelectedValue.ToString());
                    Empresa.EMP_cododbc = Convert.ToInt32(dlODBC.SelectedValue.ToString());
                    Empresa.EMP_codbasedatos = Convert.ToInt32(dlBaseDatos.SelectedValue.ToString());
                    Empresa.EMP_owner = txtOwner.Value;

                    wsgest.UPD_Empresa(Empresa);
                    //Response.Rediret("GridUsuarios.aspx");
                    Server.Transfer("frm_GridEmpresas.aspx");
                }

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }

        protected void btnSalir_Click(object sender, ImageClickEventArgs e)
        {
            Server.Transfer("frm_GridEmpresas.aspx");
        }
    }
}